    program pct_shrs_onloan
    character*6 ticker(5000)
    character*8 cusp(5000)
    character*9 temp9
    integer(kind=8) nshrs(5000),nvalue(5000),jvalue(5000),ivol(5000),numshrs(5000)
    dimension jast(5000),jdate(5000),itickt(5000),age(5000),favg(5000),fmax(5000),fmin(5000),fstd(5000),iperm(5000)
    dimension  price(5000),ishrout(5000),vwret(5000),util(5000),retrn(5000),dfee(5000),nep(5000)
!      open(unit=10,file='e:\FIS_duration\episode_data.txt')
!      open(unit=99,file='e:\FIS_duration\pct_shrs_onloan.txt')
      open(unit=10,file='e:\FIS_duration\episode_data_rep.txt')
      open(unit=99,file='e:\FIS_duration\pct_shrs_onloan_rep.txt')
      iend=0
      i=0
1     i=i+1      
      read(10,1000,end=900)nep(i),jast(i),jdate(i),cusp(i),ticker(i),itickt(i),numshrs(i),age(i),favg(i),fmax(i),fmin(i),fstd(i),iperm(i),price(i),ivol(i),retrn(i),ishrout(i),vwret(i),util(i),nshrs(i)     
1000  format(1x,i4,2(1x,i8),1x,a8,2x,a6,4x,i9,1x,i14,5(1x,f11.4),1x,i5,1x,f9.2,1x,i10,1x,f10.6,1x,i9,1x,f9.6,1x,f6.2,1x,i13) 
      if(nep(i).eq.nep(1)) go to 1
5     icnt=i-1
!      if(icnt.lt.3) go to 500
      icntm=icnt-1
      pctmin=9.99
      pctmax=-9.9
      totpct=0.
      do 40 ii=1,icnt
          im=ii-1
          if(ishrout(ii).lt.1) ishrout(ii)=ishrout(im)
          perct=float(numshrs(ii))/(float(ishrout(ii))*1000.)
          totpct=totpct+perct
          if(perct.lt.pctmin) pctmin=perct
          if(perct.gt.pctmax) pctmax=perct
40    continue
      avgpct=totpct/(float(icnt))
      itype=2
      if(favg(icnt).lt.2.and.favg(icntm).lt.2) itype=1
      if(jdate(icnt).eq.20231229) itype=3
      write(6,6000)nep(1),itype,icnt,pctmin,pctmax,avgpct
      write(99,6000)nep(1),itype,icnt,pctmin,pctmax,avgpct
6000  format(1x,i4,1x,i1,1x,i4,4(1x,f10.6))      
500   jast(1)=jast(i)
      jdate(1)=jdate(i)
      cusp(1)=cusp(i)
      ticker(1)=ticker(i)
      itickt(1)=itickt(i)
      numshrs(1)=numshrs(i)
      jvalue(1)=jvalue(i)
      age(1)=age(i)
      favg(1)=favg(i)
      fmax(1)=fmax(i)
      fmin(1)=fmin(i)
      fstd(1)=fstd(i)
      iperm(1)=iperm(i)
      price(1)=price(i)
      ivol(1)=ivol(i)
      retrn(1)=retrn(i)
      ishrout(1)=ishrout(i)
      vwret(1)=vwret(i)
      util(1)=util(i)
      nshrs(1)=nshrs(i)
      nvalue(1)=nvalue(i) 
      dfee(1)=dfee(i)
      nep(1)=nep(i)
      i=1
      go to 1
900   iend=iend+1
      if(iend.eq.1) go to 5 
      stop

    end program pct_shrs_onloan
